<script>
	import {
		login,
		findPhone
	} from './api/api.js'
	export default {
		data() {
			return {
				isWeixin: false,
				isPhone: true, //是否绑定手机号
			}
		},
		onShow: function() {
			let that = this
			// 判断token是否存在如果不存在静默登录获取token
			let water = uni.getStorage({
				key: 'accessToken',
				complete(res) {
					if (!res.data) {
						// 判断是否是微信内置浏览器
						that.isWeixin = that.isWechat()
						if (that.isWeixin) {
							that.checkWeChatCode() //通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
						}

					} else {
						findPhone(res.data.accessToken).then(result => {
							console.log('findPhone', result)
							if (result.data) {
								if (result.data.user_role == '1') {
									that.$store.commit("userInfo", result.data)
								}else{
									that.$store.commit("userInfo", result.data)
								}
								that.$store.commit("isPhone", false)
							} else {
								that.$store.commit("isPhone", true)
							}
						})
						// 获取最新的时间戳，用来判断token是否过期
						let newTime = new Date().getTime()
						console.log('最新的时间戳', newTime)
						if (res.data.tokenTime <= newTime) {
							console.log('token过期', res.data.tokenTime <= newTime)
							uni.removeStorage({
								key: 'accessToken',
								complete(res) {
									console.log('token过期', res)
									// 判断是否是微信内置浏览器
									that.isWeixin = that.isWechat()
									console.log('that.isWeixin', that.isWeixin)
									if (that.isWeixin) {
										that.checkWeChatCode() //通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
									}
								}
							})
						}
					}
				}
			})
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			//方法：用来判断是否是微信内置的浏览器
			isWechat() {
				console.log('微信浏览器判断')
				return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
			},
			//方法:用来提取code
			getUrlCode(name) {
				console.log('提取code')
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
					[, ''
					])[1]
					.replace(/\+/g, '%20')) || null
			},
			//检查浏览器地址栏中微信接口返回的code
			checkWeChatCode() {
				console.log('监测微信浏览器是否有CODE')
				let code = this.getUrlCode('code')
				if (code) {
					this.getOpenidAndUserinfo(code)
				} else {
					this.getWeChatCode()
				}
			},
			//请求微信接口，用来获取code
			getWeChatCode() {
				console.log('有code')
				let local = encodeURIComponent('https://lxsw.pcippc.com/'); //获取当前页面地址作为回调地址
				// let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
				let appid = 'wx873a10f022021245'
				// let appid = 'wx6783cd3f09d05cc5'//测试
				//通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
				// 非静默
				// window.location.href =
				// 	`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect;`
				// 静默
				window.location.href =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=snsapi_base&state=1#wechat_redirect;`

			},
			// 如果有code自己登录
			async getOpenidAndUserinfo(code) {
				console.log('有code')
				let result = await login(code)
				console.log('登录接口', result)
				let isPhone = await findPhone(result.data.access_token)
				console.log('isPhone', isPhone)
				if (isPhone.data) {
					this.$store.commit("isPhone", false)
					if (isPhone.data.user_role == '1') {
						this.$store.commit("userInfo", isPhone.data)
					} else {
						this.$store.commit("userInfo", isPhone.data)
					}
				} else {
					this.$store.commit("isPhone", true)
				}
				let tokenTime = new Date().getTime() + 7200000
				console.log('过期时间', tokenTime)
				uni.setStorage({
					key: 'accessToken',
					data: {
						accessToken: result.data.access_token,
						tokenTime: tokenTime
					},
				})
			},
		},
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	/*每个页面公共css */
</style>